package org.xutils.xutillearn.ui.adapter;

import android.app.Activity;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.content.pm.ResolveInfo;
import android.support.design.widget.Snackbar;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import org.xutils.xutillearn.R;
import org.xutils.xutillearn.bean.LearnListItem;
import org.xutils.xutillearn.ui.activity.RecyclerViewActivity;
import org.xutils.xutillearn.ui.listener.RecyclerItemListener;

import java.util.List;

/**
 * Created by yangmuyi on 16/4/12.
 */
public class MyAdapter extends RecyclerView.Adapter<MyAdapter.MyViewHolder>{

    public List<LearnListItem> list;
    private Context context;
    public MyAdapter(List<LearnListItem> list,Context context){
        this.list = list;
        this.context = context;
    }

    @Override
    public MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        MyViewHolder holder = new MyViewHolder(View.inflate(context,R.layout.item_recyclerview,null));
        return holder;
    }

    @Override
    public void onBindViewHolder(MyViewHolder holder, int position) {
        LearnListItem item = list.get(position);
        holder.mtextView.setText(item.getName());
        holder.item = list.get(position);
    }

    @Override
    public int getItemCount() {
        return list.size();
    }


    @Override
    public long getItemId(int position) {
        return position;
    }



    class MyViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener{
        private View v;
        private TextView mtextView;
        private LearnListItem item;


        public MyViewHolder(View v){
            super(v);
            mtextView = (TextView)v.findViewById(R.id.textview);
            v.setOnClickListener(this);

        }

        @Override
        public void onClick(View view) {
            Intent intent = new Intent();
            intent.setClassName(item.getPkgName(),item.getClassName());
            boolean isExist = checkActivit(intent);


            if(isExist){
                context.startActivity(intent);
            }else{
                Snackbar.make(view,"this is a null Activity",Snackbar.LENGTH_SHORT).show();
            }
        }


        public boolean checkActivit(Intent intent){
            ResolveInfo resolveInfo = context.getPackageManager().resolveActivity(intent, PackageManager.COMPONENT_ENABLED_STATE_DEFAULT);
            if(resolveInfo==null){
                return false;
            }else{
                return true;
            }
        }

        public View getV() {
            return v;
        }

        public void setV(View v) {
            this.v = v;
        }

        public TextView getMtextView() {
            return mtextView;
        }

        public void setMtextView(TextView mtextView) {
            this.mtextView = mtextView;
        }

        public LearnListItem getItem() {
            return item;
        }

        public void setItem(LearnListItem item) {
            this.item = item;
        }
    }
}
